Ext.define('TS.lib.LoginForm',{
	extend:'TS.lib.Form',
	alias:'widget.loginform',
	fieldDefaults:{
		labelAlign:'left',
		labelWidth: 80
	},
	url:URL.util.login,
    items:[
        //{name:'akcjaLoguj',v:1}),//co to jest?pozostalosc.
        {xtype:'text',fieldLabel:'Login',name:'login',onBlur:function(x,val){
        	var s_login = Ext.getDom('sweet_login');
        	var s_pass = Ext.getDom('sweet_pass');
        	s_login.focus();
        	s_login.value = Util.fld('login->#login').getValue();
        	s_pass.focus();
        	var pass = Util.fld('login->#password');
        	pass.setValue(s_pass.value);
        	pass.focus();
        },onEnter:Util.fireButton('login','loginform')},
        {xtype:'text',fieldLabel:'Hasło',name:'password',inputType:'password',onEnter:Util.fireButton('login','loginform')}
    ],
    buttons:[{
    	text:'Zaloguj',
    	action:'login',
    	icon:SERVER.base_url+"/img/icons/bullet_go.png",
    	handler:function(btn,e){

        	var f = btn.findParentByType('form').getForm();
			if (f.isValid()){
				
				var s_login = Ext.getDom('sweet_login');
	        	var s_pass = Ext.getDom('sweet_pass');
	        	s_login.focus();
	        	s_login.value = Util.fld('login->#login').getValue();
	        	s_pass.focus();
	        	s_pass.value = Util.fld('login->#password').getValue();
	        	Ext.getDom('submit').click();
				
				f.submit({
					success:function(form,req){
						r = Ext.decode(req.response.responseText);
						if (r.success){
							//var prev_role = USER.user_role_id;
							APP.session.check();
							//APP.afterLogin(prev_role);
						} else {// nigdy tu niewchodzi
							if(r.reason && r.reason == 'new_version') Factory.flyer.bad(r.msg);
							else Factory.flyer.bad('Błąd logowania');
						}
					},
					failure:function(form,req){
						r = Ext.decode(req.response.responseText);
						if (r.success){ 
							//var prev_role = USER.user_role_id;
							//My.utils.session_check();
							//APP.afterLogin(prev_role);
						} else { // succes false zawsze tu trafia.
							if(r.reason && r.reason == 'new_version') Factory.flyer.bad(r.msg);
							else Factory.flyer.bad('Błąd logowania');
						}
					}
				});
			} else Factory.flyer.bad('Wypełnij poprawnie formularz.');
        }
    },{
    	text:'Zamknij',
    	handler:function(){
    		this.findParentByType('window').close();// lub Util.close('login');
    	}
    }]
});

Ext.define('TS.lib.LoginWindow', {
	extend:'TS.lib.Window',
	alias:'widget.loginwindow',
	width:300,
	title:'Logowanie do Timesheet 8.4',
	items:[{xtype:'loginform'}]
});
